#include<iostream>
#include <sys/epoll.h>
#include "log.hpp"
#include <string.h>
#include "nocopy.hpp"
//epoll等待类:对epoll操作进行封装
class epoller  : public nocopy     
{
static const int size=128;
public:
    epoller()
    :timeout(3000)
    {
        _epfd=epoll_create(size);
        if (_epfd == -1)
        {
            lg(Error, "epoll_create error: %s", strerror(errno));
        }
        else
        {
            lg(Info, "epoll_create success: %d", _epfd);
        }
    }
    ~epoller()
    {
        close(_epfd);
    }

    int Epoll_Update(int op,int fd, uint32_t event)
    {
        //删除fd
        int n=0;
        if(op==EPOLL_CTL_DEL)
        {
            n=epoll_ctl(_epfd,op,fd,nullptr);  //第四个参数监听时才需要
            if(n<0)
            {
               lg(Error, "epoll_ctl delete error!");
            }

        }
        else
        {
            struct epoll_event ev;
            ev.events = event;     //监听事件类型  读、写等   如可读、可写、异常等事件
            ev.data.fd = fd;   //监听事件的属性       
            n=epoll_ctl(_epfd,op,fd,&ev);
            if(n<0)
            {
               lg(Error, "epoll_ctl read or write error!");
            }
        }
        return n;
    }

    int Epoll_Wait(struct epoll_event*event,int maxevent)
    {
        int n=epoll_wait(_epfd,event,maxevent,timeout);
        if(n<0)
        {
            lg(Error, "epoll_wait  error!");
        }
        return n;

    }

private:
    int _epfd;
    int timeout;
};

